<?php 
error_reporting(E_ALL);

// get full path from server, prevent errors
chdir(dirname(__FILE__));

class seo_engine
{
	private $cache_folder 		= "plugins/cache"; // cache file for plugins

	// curl settings
	private $referral 			= "http://google.com"; // referral when using curl
	
	// path for plugins
	const PLUGIN_PATH = 'plugins';	

	function __construct()
	{

	}

	function __destruct()
	{

	}

	
	public function get_plugin_path()
	{
		return self::PLUGIN_PATH;
	}
	

	public function plugin_list()
	{
		$dir = self::PLUGIN_PATH;
		$dh  = opendir($dir);

		if(!$dh)
		{
			echo "Error opening the plugins directory!";
			return false;
		}

		// only check for valid plugins
		while (false !== ($filename = readdir($dh)))
		{
			if(preg_match('#plugin_(\S+)#',$filename, $findPlugin))
		    	$files[substr($findPlugin[1], 0, -4)] = $filename;
		}

		return $files;
	}

	public function doRequest( $url, $post = false)
	{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			
			// custom referral
			curl_setopt($ch, CURLOPT_REFERER, $this->referral);

			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			curl_setopt($ch, CURLOPT_TIMEOUT, 18);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);

			curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1");
			curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");		
			curl_setopt($ch, CURLOPT_URL, $url);
			
			if( is_array($post) && count($post) > 0){
				curl_setopt($ch, CURLOPT_POST, true);
				curl_setopt($ch, CURLOPT_POSTFIELDS , $post);
			}
			
			if(curl_exec($ch) === false){
	   			echo 'Curl error: ' . curl_error($ch);
			}
			
			$strResponse = curl_exec($ch);
			
			return $strResponse;
	}

	/*
			
	*/
	public static function trimt_to_root($url)
	{
		if( substr($url, 0,4) == 'http')
			$x = parse_url($url); // only when http:// exists
		else
			return false;

		//attempt to get the domain
		if( isset($x['host']) )
			return $x['host'];
	}
}

$seo = new seo_engine;


?>